package com.xingshentech.ugv.operate.module.httpdata;

import com.xingshentech.ugv.operate.base.ApplicationShare;
import com.xstech.library.app.BaseApp;
import com.xstech.library.rxjavahttp.ApiAddress;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitUtil {
//    包含 单例模式、动态 Base URL、JSON 解析 等功能
    /**
     * 超时时间
     */
    private static volatile RetrofitUtil mInstance;
    private MapApi mapApi;
    private CenterApi centerApi;
    private VersionApi mVersionApi;
    private MonitorApi mMonitorApi;
    private OtaApi mOtaApi;

    /**
     * 单例封装
     *
     * @return
     */
    public static RetrofitUtil getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitUtil.class) {
                if (mInstance == null) {
                    mInstance = new RetrofitUtil();
                }
            }
        }
        return mInstance;
    }


    /**
     * 初始化Retrofit
     */
    public CenterApi initRetrofit() {
        if (centerApi == null) {
//              获取 Retrofit 单例
            Retrofit mRetrofit = new Retrofit.Builder()
//                   配置 OkHttpClient
                    .client(ApplicationShare.initOKHttp())
                    // 设置请求的域名
                    .baseUrl(ApiAddress.api_center)
                    // 使用 Gson 解析 JSON
                    .addConverterFactory(GsonConverterFactory.create())
//                    让 Retrofit 支持 RxJava，从而可以使用 Observable、Flowable、Single、
//                    Maybe、Completable 处理网络请求
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            centerApi = mRetrofit.create(CenterApi.class);
        }
        return centerApi;
    }


    /**
     * 初始化Retrofit
     */
    public VersionApi initRetrofitVersion() {
        if (mVersionApi == null) {
            Retrofit mRetrofit = new Retrofit.Builder()
                    .client(ApplicationShare.initOKHttp2())
                    // 设置请求的域名
                    .baseUrl(ApiAddress.versionApi)
                    // 设置解析转换工厂，用自己定义的
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            mVersionApi = mRetrofit.create(VersionApi.class);
        }
        return mVersionApi;
    }

    public MonitorApi initRetrofitMonitor() {
        if (mMonitorApi == null) {
            Retrofit mRetrofit = new Retrofit.Builder()
                    .client(ApplicationShare.initOKHttp())
                    // 设置请求的域名
                    .baseUrl(ApiAddress.apiMonitor)
                    // 设置解析转换工厂，用自己定义的
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            mMonitorApi = mRetrofit.create(MonitorApi.class);
        }
        return mMonitorApi;
    }

    public OtaApi initOta() {
        if (mOtaApi == null) {
            Retrofit mRetrofit = new Retrofit.Builder()
                    .client(ApplicationShare.initOKHttp())
                    // 设置请求的域名
                    .baseUrl(ApiAddress.api_ota)
                    // 设置解析转换工厂，用自己定义的
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            mOtaApi = mRetrofit.create(OtaApi.class);
        }
        return mOtaApi;
    }

}

